//分类相关仓库
import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
  reqCategory1API,
  reqCategory2API,
  reqCategory3API,
} from '@/apis/product/attr'
import type { Category, CategoryResponseData } from '@/apis/product/attr/type'

const useCategoryStore = defineStore('Category', () => {
  //一级分类数据
  const category1 = ref<Category[]>([])
  const c1Id = ref<number | string>('')
  //二级分类数据
  const category2 = ref<Category[]>([])
  const c2Id = ref<number | string>('')
  //三级分类数据
  const category3 = ref<Category[]>([])
  const c3Id = ref<number | string>('')
  //获取一级分类数据
  const getCategory1 = async () => {
    let result: CategoryResponseData = await reqCategory1API()
    if (result.code === 200) {
      category1.value = result.data
    }
  }
  //获取二级分类数据
  const getCategory2 = async (c1Id: number|string) => {
    let result: CategoryResponseData = await reqCategory2API(c1Id)
    if (result.code === 200) {
      category2.value = result.data
    }
  }
  //获取三级分类数据
  const getCategory3 = async (c2Id: number|string) => {
    let result: CategoryResponseData = await reqCategory3API(c2Id)
    if (result.code === 200) {
      category3.value = result.data
    }
  }
  return {
    category1,
    c1Id,
    category2,
    c2Id,
    category3,
    c3Id,
    getCategory1,
    getCategory2,
    getCategory3,
  }
})
export default useCategoryStore
